﻿using System;
using System.IO;
using System.Text;
//using Learun.Util;
//using System.IO;
//using Learun.Util.Office2Pdf;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.pdf.codec;

namespace Learun.Application.OA.File.FilePreview
{
    public class FilePreviewBLL : FilePreviewIBLL
    {
        //Office2Pdf office2Pdf = new Office2Pdf();

        /// <summary>
        /// 获取EXCEL数据
        /// <summary>
        /// <returns></returns>
        public void GetExcelData(string path)
        {
            try
            {
                //office2Pdf.ExcelConvertPDF(path);//此处可在服务器安装office2007通过offic2007转换
                //load Excel file
                //Workbook workbook = new Workbook();
                //workbook.LoadFromFile(path);
                //workbook.SaveToFile(path.Substring(0, path.LastIndexOf(".")) + ".pdf", Spire.Xls.FileFormat.PDF);
            }
            catch (Exception)
            {
                throw (new Exception("文件丢失"));
            }
        }


        /// <summary>
        /// 获取Word数据
        /// <summary>
        /// <returns></returns>
        public void GetWordData(string path)
        {
            try
            {
                //office2Pdf.WordConvertPDF(path);此处可在服务器安装office2007通过offic2007转换
                //Document document = new Document();
                //document.LoadFromFile(path);
                //document.SaveToFile(path.Substring(0, path.LastIndexOf(".")) + ".pdf", Spire.Doc.FileFormat.PDF);               
            }
            catch (Exception)
            {
                throw (new Exception("文件丢失"));
            }
        }
        /// <summary>
        /// 获取PPT数据
        /// <summary>
        /// <returns></returns>
        public void GetPptData(string path)
        {
            try
            {
                // office2Pdf.PPTConvertPDF(path);office2Pdf.WordConvertPDF(path);此处可在服务器安装office2007通过offic2007转换
                //Document document = new Document();
                //document.LoadFromFile(path);
                //document.SaveToFile(path.Substring(0, path.LastIndexOf(".")) + ".pdf", Spire.Doc.FileFormat.PDF);
            }
            catch (Exception ex)
            {
                throw (new Exception("文件丢失"));
            }
        }
        /// <summary>
        /// 将Pptx文档转换为HTML
        /// <summary>
        /// <returns></returns>
        public void ConvertPptxToHtml(string oldPath, string newPath)
        {
            try
            {
            }
            catch (Exception ex)
            {
                throw (new Exception("文件丢失"));
            }
        }


        /// <summary>
        /// 获取PPT数据
        /// <summary>
        /// <returns></returns>
        public void GetTifData(string sFileTif)
        {
            try
            {
                string newPdfPath = sFileTif.Substring(0, sFileTif.LastIndexOf(".")) + ".pdf";
                System.IO.FileInfo _toFile = new System.IO.FileInfo(newPdfPath);
                // 创建一个文档对象
                iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A2, 0, 0, 0, 0);
                int pages = 0;
                FileStream fs = new FileStream(newPdfPath, FileMode.OpenOrCreate);
                // 定义输出位置并把文档对象装入输出对象中
                PdfWriter writer = PdfWriter.GetInstance(doc, fs);
                // 打开文档对象
                doc.Open();

                PdfContentByte cb = writer.DirectContent;
                RandomAccessFileOrArray ra = new RandomAccessFileOrArray(sFileTif);
                int comps = TiffImage.GetNumberOfPages(ra);
                for (int c = 0; c < comps; ++c)
                {
                    Image img = TiffImage.GetTiffImage(ra, c + 1);
                    if (img != null)
                    {
                        img.ScalePercent(7200f / img.DpiX, 7200f / img.DpiY);
                        doc.SetPageSize(new Rectangle(img.ScaledWidth, img
                         .ScaledHeight));
                        img.SetAbsolutePosition(0, 0);
                        cb.AddImage(img);
                        doc.NewPage();
                        ++pages;
                    }
                }
                ra.Close();// 关闭
                // 关闭文档对象，释放资源
                doc.Close();
            }
            catch (Exception)
            {
                throw (new Exception("文件丢失"));
            }
        }
        public void GetTxtData(string sFileTxt) {
            try
            {
                string[] lines = System.IO.File.ReadAllLines(sFileTxt, Encoding.GetEncoding("utf-8"));
                iTextSharp.text.Document doc = new iTextSharp.text.Document(PageSize.A2, 0, 0, 0, 0);
                PdfWriter pdfwriter =
                    PdfWriter.GetInstance(doc, new FileStream(sFileTxt.ToString().Substring(0, sFileTxt.LastIndexOf(".")) + ".pdf", FileMode.Create));

                doc.Open();
                //创建我的基本字体  
                BaseFont baseFont = BaseFont.CreateFont(System.Environment.GetEnvironmentVariable("windir") + @"\fonts\simsun.ttc,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);

                //创建字体      字体大小，字体粗細    字体颜色  
                Font font = new Font(baseFont, 11, iTextSharp.text.Font.NORMAL, BaseColor.BLACK);

                Paragraph paragraph;
                foreach (string line in lines)
                {
                    paragraph = new Paragraph(line, font);
                    doc.Add(paragraph);
                }

                //关闭文件  
                doc.Close();
            } catch (Exception) {
                throw (new Exception("文件丢失"));
            }
        }
    }
}